﻿using PEProtocol;

/**
 * 任务系统
 */
public class TaskSys{
    private static TaskSys instance = null;

    public static TaskSys Instance{
        get{
            if (instance == null){
                instance = new TaskSys();
            }

            return instance;
        }
    }

    private CacheSvc cacheSvc = null;
    private CfgSvc cfgSvc = null;

    public void Init(){
        cacheSvc = CacheSvc.Instance;
        cfgSvc = CfgSvc.Instance;
        PECommon.Log("TaskSys Init done");
    }

    public void TakeTaskReward(MsgPack pkt){
        ReqTakeTaskReward data = pkt.msg.reqTakeTaskReward;

        GameMsg msg = new GameMsg{
            cmd = (int)CMD.ResTakeTaskReward
        };

        PlayerData pd = cacheSvc.GetPlayerDataBySession(pkt.session);

        TaskRewardCfg trc = cfgSvc.GetTaskCfg(data.rid);
        TaskRewardData trd = CalcTaskRewardData(pd, data.rid);

        if (trd.prg == trc.count && !trd.taked){
            pd.coin += trc.coin;
            PECommon.CalcExp(pd, trc.exp);
            trd.taked = true;
            //更新任务进度数据
            CalcTaskArr(pd, trd);

            if (!cacheSvc.UpdatePlayerData(pd.id, pd)){
                msg.err = (int) ErrorCode.UpdateDBError;
            }
            else{
                ResTakeTaskReward resTakeTaskReward = new ResTakeTaskReward{
                    coin = pd.coin,
                    lv = pd.level,
                    exp = pd.exp,
                    taskArr = pd.taskArr
                };
                msg.resTakeTaskReward = resTakeTaskReward;
            }
        }
        else{
            msg.err = (int) ErrorCode.ClientDataError;
        }
        
        pkt.session.SendMsg(msg);
    }

    public TaskRewardData CalcTaskRewardData(PlayerData pd, int rid){
        TaskRewardData trd = new TaskRewardData();
        for (int i = 0; i < pd.taskArr.Length; i++){
            string[] taskInfo = pd.taskArr[i].Split('|');
            //1|0|0
            if (int.Parse(taskInfo[0]) == rid){
                trd = new TaskRewardData{
                    ID = int.Parse(taskInfo[0]),
                    prg = int.Parse(taskInfo[1]),
                    taked = taskInfo[2].Equals("1")
                };
                break;
            }
        }

        return trd;
    }

    public void CalcTaskArr(PlayerData pd, TaskRewardData trd){
        string result = trd.ID + "|" + trd.prg + "|" + (trd.taked ? 1 : 0);
        int index = -1;
        for (int i = 0; i < pd.taskArr.Length; i++){
            string[] taskInfo = pd.taskArr[i].Split('|');
            if (int.Parse(taskInfo[0]) == trd.ID){
                index = i;
                break;
            }
        }

        pd.taskArr[index] = result;
    }

    public void CalcTaskPrgs(PlayerData pd, int tid){
        TaskRewardData trd = CalcTaskRewardData(pd, tid);
        TaskRewardCfg trc = cfgSvc.GetTaskCfg(tid);

        if (trd.prg < trc.count){
            trd.prg += 1;
            //更新任务进度
            CalcTaskArr(pd, trd);

            ServerSession session = cacheSvc.GetOnlineServerSession(pd.id);
            if (session != null){
                session.SendMsg(new GameMsg{
                    cmd = (int)CMD.PshTaskPrg,
                    pshTaskPrg = new PshTaskPrg{
                        taskArr = pd.taskArr
                    }
                });
            }
        }
    }
    
    public PshTaskPrg GetTaskPrgs(PlayerData pd, int tid){
        TaskRewardData trd = CalcTaskRewardData(pd, tid);
        TaskRewardCfg trc = cfgSvc.GetTaskCfg(tid);

        if (trd.prg < trc.count){
            trd.prg += 1;
            //更新任务进度
            CalcTaskArr(pd, trd);

            return new PshTaskPrg{
                taskArr = pd.taskArr
            };
        }
        else{
            return null;
        }
    }
}